<!DOCTYPE html>
<html lang="zh-CN">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width,initial-scale=1">
    <title>PowerShell | lilei</title>
    <meta name="generator" content="VuePress 1.8.2">
    <link rel="icon" href="/images/icons/logo.png">
    <link rel="apple-touch-icon" href="/images/icons/logo.png">
    <link rel="manifest" href="/manifest.json">
    <script type="text/javascript" src="/assets/js/load.js"></script>
    <meta name="description" content="个人网站">
    <meta name="twitter:title" content="PowerShell">
    <meta name="twitter:description" content="">
    <meta name="twitter:card" content="summary">
    <meta name="twitter:url" content="http://www.lileiblog.xyz/Shell/PowerShell.html">
    <meta property="og:type" content="article">
    <meta property="og:title" content="PowerShell">
    <meta property="og:description" content="">
    <meta property="og:url" content="http://www.lileiblog.xyz/Shell/PowerShell.html">
    <meta property="og:site_name" content="lilei">
    <meta itemprop="name" content="PowerShell">
    <meta itemprop="description" content="">
    <meta name="theme-color" content="#3eaf7c">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <meta name="msapplication-TileImage" content="/images/icons/logo.png">
    <meta name="msapplication-TileColor" content="#ffffff">
    <meta name="keywords" content="lilei,vue,vuepress,vuepress-theme,theme,主题,vuepress主题,blog
        ,vuepress-blog,java,python,shell,sql,golang,script,shell script,nginx,windows,去广告,androd,google drive
        ,one drive,idea,eclipse,git,小程序">
    <meta name="referrer" content="never">
    
    <link rel="preload" href="/assets/css/0.styles.8932179b.css" as="style"><link rel="preload" href="/assets/js/app.269009d6.js" as="script"><link rel="preload" href="/assets/js/3.974b9720.js" as="script"><link rel="preload" href="/assets/js/56.f29549a3.js" as="script"><link rel="prefetch" href="/assets/js/10.5915e99b.js"><link rel="prefetch" href="/assets/js/11.ff732762.js"><link rel="prefetch" href="/assets/js/12.8bb86d91.js"><link rel="prefetch" href="/assets/js/13.18bde5b0.js"><link rel="prefetch" href="/assets/js/14.314fae34.js"><link rel="prefetch" href="/assets/js/15.0a4ff48f.js"><link rel="prefetch" href="/assets/js/16.ffaf1503.js"><link rel="prefetch" href="/assets/js/17.49a31a6e.js"><link rel="prefetch" href="/assets/js/18.7e6443bf.js"><link rel="prefetch" href="/assets/js/19.6880cc9a.js"><link rel="prefetch" href="/assets/js/20.1fb3f255.js"><link rel="prefetch" href="/assets/js/21.0cd39eea.js"><link rel="prefetch" href="/assets/js/22.392d85cb.js"><link rel="prefetch" href="/assets/js/23.f50adb15.js"><link rel="prefetch" href="/assets/js/24.43e324e1.js"><link rel="prefetch" href="/assets/js/25.e3663205.js"><link rel="prefetch" href="/assets/js/26.da141fb8.js"><link rel="prefetch" href="/assets/js/27.6c05235a.js"><link rel="prefetch" href="/assets/js/28.c124245a.js"><link rel="prefetch" href="/assets/js/29.d185870d.js"><link rel="prefetch" href="/assets/js/30.8587d422.js"><link rel="prefetch" href="/assets/js/31.dc382b56.js"><link rel="prefetch" href="/assets/js/32.20ba32e9.js"><link rel="prefetch" href="/assets/js/33.6ecd7de9.js"><link rel="prefetch" href="/assets/js/34.2b91c13a.js"><link rel="prefetch" href="/assets/js/35.66ed0733.js"><link rel="prefetch" href="/assets/js/36.ce7bca84.js"><link rel="prefetch" href="/assets/js/37.d00bb5ac.js"><link rel="prefetch" href="/assets/js/38.c3ecbb6f.js"><link rel="prefetch" href="/assets/js/39.308f4175.js"><link rel="prefetch" href="/assets/js/4.520f6ecc.js"><link rel="prefetch" href="/assets/js/40.f4b98bc2.js"><link rel="prefetch" href="/assets/js/41.95fae64f.js"><link rel="prefetch" href="/assets/js/42.fe4f877b.js"><link rel="prefetch" href="/assets/js/43.ca8f222e.js"><link rel="prefetch" href="/assets/js/44.4d4ee79e.js"><link rel="prefetch" href="/assets/js/45.79178b33.js"><link rel="prefetch" href="/assets/js/46.c3817d0b.js"><link rel="prefetch" href="/assets/js/47.b8252443.js"><link rel="prefetch" href="/assets/js/48.df9d5d3b.js"><link rel="prefetch" href="/assets/js/49.d812fdb2.js"><link rel="prefetch" href="/assets/js/5.f1a24a16.js"><link rel="prefetch" href="/assets/js/50.55cfcaa8.js"><link rel="prefetch" href="/assets/js/51.b8608b02.js"><link rel="prefetch" href="/assets/js/52.52c5e00d.js"><link rel="prefetch" href="/assets/js/53.3a9bd5fb.js"><link rel="prefetch" href="/assets/js/54.a4240c13.js"><link rel="prefetch" href="/assets/js/55.01c4b876.js"><link rel="prefetch" href="/assets/js/57.01438a04.js"><link rel="prefetch" href="/assets/js/58.29bb4626.js"><link rel="prefetch" href="/assets/js/59.e8b89408.js"><link rel="prefetch" href="/assets/js/6.8c478b39.js"><link rel="prefetch" href="/assets/js/60.bedb5381.js"><link rel="prefetch" href="/assets/js/61.bdfbe584.js"><link rel="prefetch" href="/assets/js/62.cb825d1a.js"><link rel="prefetch" href="/assets/js/63.a9516572.js"><link rel="prefetch" href="/assets/js/64.aa854ed7.js"><link rel="prefetch" href="/assets/js/65.285f883d.js"><link rel="prefetch" href="/assets/js/66.12849f37.js"><link rel="prefetch" href="/assets/js/67.c18b00e4.js"><link rel="prefetch" href="/assets/js/68.fa59daa9.js"><link rel="prefetch" href="/assets/js/69.e9d5dda4.js"><link rel="prefetch" href="/assets/js/7.cfb0d0ea.js"><link rel="prefetch" href="/assets/js/70.db126cce.js"><link rel="prefetch" href="/assets/js/71.6acb04cd.js"><link rel="prefetch" href="/assets/js/72.7ff0ab11.js"><link rel="prefetch" href="/assets/js/73.9a66da79.js"><link rel="prefetch" href="/assets/js/74.0a38a030.js"><link rel="prefetch" href="/assets/js/75.7b32dbb3.js"><link rel="prefetch" href="/assets/js/8.cb9c08e5.js"><link rel="prefetch" href="/assets/js/9.ad652cfe.js"><link rel="prefetch" href="/assets/js/vendors~flowchart.110a1065.js">
    <link rel="stylesheet" href="/assets/css/0.styles.8932179b.css">
  </head>
  <body>
    <div id="app" data-server-rendered="true"><div class="theme-container"><header class="navbar"><div class="sidebar-button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" role="img" viewBox="0 0 448 512" class="icon"><path fill="currentColor" d="M436 124H12c-6.627 0-12-5.373-12-12V80c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12z"></path></svg></div> <a href="/" class="home-link router-link-active"><img src="/images/icons/logo.png" alt="lilei" class="logo"> <span class="site-name can-hide">lilei</span></a> <div class="links"><div class="search-box"><input aria-label="Search" autocomplete="off" spellcheck="false" value=""> <!----></div> <nav class="nav-links can-hide"><div class="nav-item"><a href="/Form/" class="nav-link">
  Form
</a></div><div class="nav-item"><a href="/IDE/" class="nav-link">
  IDE
</a></div><div class="nav-item"><a href="/Java/" class="nav-link">
  Java
</a></div><div class="nav-item"><a href="/Other/" class="nav-link">
  Other
</a></div><div class="nav-item"><a href="/Python/" class="nav-link">
  Python
</a></div><div class="nav-item"><a href="/SQL/" class="nav-link">
  SQL
</a></div><div class="nav-item"><a href="/Shell/" class="nav-link router-link-active">
  Shell
</a></div><div class="nav-item"><a href="/System/" class="nav-link">
  System
</a></div> <a href="https://github.com/woytu/notes-vuepress" target="_blank" rel="noopener noreferrer" class="repo-link">
    GitHub
    <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></nav></div></header> <div class="sidebar-mask"></div> <aside class="sidebar"><nav class="nav-links"><div class="nav-item"><a href="/Form/" class="nav-link">
  Form
</a></div><div class="nav-item"><a href="/IDE/" class="nav-link">
  IDE
</a></div><div class="nav-item"><a href="/Java/" class="nav-link">
  Java
</a></div><div class="nav-item"><a href="/Other/" class="nav-link">
  Other
</a></div><div class="nav-item"><a href="/Python/" class="nav-link">
  Python
</a></div><div class="nav-item"><a href="/SQL/" class="nav-link">
  SQL
</a></div><div class="nav-item"><a href="/Shell/" class="nav-link router-link-active">
  Shell
</a></div><div class="nav-item"><a href="/System/" class="nav-link">
  System
</a></div> <a href="https://github.com/woytu/notes-vuepress" target="_blank" rel="noopener noreferrer" class="repo-link">
    GitHub
    <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></nav>  <ul class="sidebar-links"><li><section class="sidebar-group depth-0"><p class="sidebar-heading open"><span>Shell</span> <!----></p> <ul class="sidebar-links sidebar-group-items"><li><a href="/Shell/" aria-current="page" class="sidebar-link">Shell</a></li><li><a href="/Shell/PowerShell.html" aria-current="page" class="active sidebar-link">PowerShell</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header"><a href="/Shell/PowerShell.html#flag" class="sidebar-link">Flag</a></li><li class="sidebar-sub-header"><a href="/Shell/PowerShell.html#包管理" class="sidebar-link">包管理</a></li><li class="sidebar-sub-header"><a href="/Shell/PowerShell.html#在batch中嵌入运行" class="sidebar-link">在batch中嵌入运行</a></li><li class="sidebar-sub-header"><a href="/Shell/PowerShell.html#windows10自带应用" class="sidebar-link">Windows10自带应用</a></li><li class="sidebar-sub-header"><a href="/Shell/PowerShell.html#命令" class="sidebar-link">命令</a></li><li class="sidebar-sub-header"><a href="/Shell/PowerShell.html#随机字符串" class="sidebar-link">随机字符串</a></li></ul></li><li><a href="/Shell/ShellScript.html" class="sidebar-link">ShellScript</a></li><li><a href="/Shell/ShellWindows.html" class="sidebar-link">ShellWindows</a></li><li><a href="/Shell/Shell命令.html" class="sidebar-link">Shell命令</a></li><li><a href="/Shell/WindowsBatch.html" class="sidebar-link">WindowsBatch</a></li><li><a href="/Shell/WindowsJScript.html" class="sidebar-link">WindowsJScript</a></li><li><a href="/Shell/WindowsScript.html" class="sidebar-link">WindowsScript</a></li><li><a href="/Shell/WindowsVBScript.html" class="sidebar-link">WindowsVBScript</a></li></ul></section></li></ul> </aside> <main class="page"> <div class="theme-default-content content__default"><h1 id="powershell"><a href="#powershell" class="header-anchor">#</a> PowerShell</h1> <p></p><div class="table-of-contents"><ul><li><a href="#flag">Flag</a></li><li><a href="#包管理">包管理</a></li><li><a href="#在batch中嵌入运行">在batch中嵌入运行</a></li><li><a href="#windows10自带应用">Windows10自带应用</a></li><li><a href="#命令">命令</a><ul><li><a href="#事件计划任务">事件计划任务</a></li></ul></li><li><a href="#随机字符串">随机字符串</a></li></ul></div><p></p> <h2 id="flag"><a href="#flag" class="header-anchor">#</a> Flag</h2> <blockquote><p><code>PowerShell</code>的命令叫做<code>cmdlet</code>(<code>command-let</code>), 采用了“动词-名词”的命名方式，动词部分取自于一个制定的动词集合，
名词部分则描述了命令要操作的对象。例如，<code>Get-Command</code>就是指获取<code>PowerShell</code>中所有<code>cmdlet</code>命令。</p></blockquote> <blockquote><p><code>PowerShell</code>提供对<code>COM</code>（组件对象模型）和<code>WMI</code>（Windows管理规范）的完全访问，具有丰富的控制与自动化的系统管理能力，
能够轻松地做到实时、大规模的管理系统。获取本机所有<code>COM</code>组件对象脚本 <a href="/files/Get-COM-Objects.ps1">Get-COM-Objects.ps1</a></p></blockquote> <ul><li><a href="https://github.com/janikvonrotz/awesome-powershell" target="_blank" rel="noopener noreferrer">https://github.com/janikvonrotz/awesome-powershell<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></li> <li><a href="https://github.com/PowerShell" target="_blank" rel="noopener noreferrer">https://github.com/PowerShell<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></li></ul> <ul><li><a href="https://www.zhihu.com/question/22611859/answers/updated" target="_blank" rel="noopener noreferrer">PowerShell 与 cmd 有什么不同？<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></li> <li><a href="https://www.pstips.net/why-is-powershell-powerful.html" target="_blank" rel="noopener noreferrer">PowerShell为什么强大<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></li></ul> <ul><li><a href="https://docs.microsoft.com/zh-cn/powershell" target="_blank" rel="noopener noreferrer">https://docs.microsoft.com/zh-cn/powershell<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></li> <li><a href="https://docs.microsoft.com/zh-cn/powershell/module/cimcmdlets/?view=powershell-7.1" target="_blank" rel="noopener noreferrer">PowerShell参考文档<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></li> <li><a href="https://www.pstips.net/powershell-online-tutorials" target="_blank" rel="noopener noreferrer">PowerShell 在线教程<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></li> <li><a href="https://zhuanlan.zhihu.com/p/60797627" target="_blank" rel="noopener noreferrer">Powershell入门指南(一)·PowerShell及CLI发展<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></li> <li><a href="https://zhuanlan.zhihu.com/p/60798130" target="_blank" rel="noopener noreferrer">PowerShell入门指南(二)·挑战CMD和Bash的PowerShell<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></li> <li><a href="https://zhuanlan.zhihu.com/p/76708298" target="_blank" rel="noopener noreferrer">PowerShell入门指南(三)·一门新的编程语言<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></li> <li>WMIC 已弃用替代品 <a href="https://docs.microsoft.com/zh-cn/powershell/module/microsoft.powershell.management/get-wmiobject" target="_blank" rel="noopener noreferrer">Get-WmiObject<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></li> <li>WMIC 已弃用替代品 <a href="https://docs.microsoft.com/zh-cn/powershell/module/cimcmdlets/get-ciminstance" target="_blank" rel="noopener noreferrer">Get-CimInstance<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></li></ul> <ul><li><a href="https://github.com/R3MRUM/PSDecode" target="_blank" rel="noopener noreferrer">https://github.com/R3MRUM/PSDecode<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></li></ul> <h2 id="包管理"><a href="#包管理" class="header-anchor">#</a> 包管理</h2> <ul><li><a href="https://github.com/microsoft/winget-cli" target="_blank" rel="noopener noreferrer">https://github.com/microsoft/winget-cli<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a> <ul><li><a href="https://github.com/microsoft/winget-pkgs" target="_blank" rel="noopener noreferrer">https://github.com/microsoft/winget-pkgs<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></li></ul></li> <li><a href="https://github.com/oneget/oneget" target="_blank" rel="noopener noreferrer">https://github.com/oneget/oneget<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a> <ul><li><a href="https://www.nuget.org" target="_blank" rel="noopener noreferrer">https://www.nuget.org<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></li> <li><a href="https://www.powershellgallery.com" target="_blank" rel="noopener noreferrer">https://www.powershellgallery.com<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></li></ul></li> <li><a href="https://github.com/chocolatey" target="_blank" rel="noopener noreferrer">https://github.com/chocolatey<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a> <ul><li><a href="https://chocolatey.org" target="_blank" rel="noopener noreferrer">https://chocolatey.org<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></li></ul></li> <li><a href="https://github.com/lukesampson/scoop" target="_blank" rel="noopener noreferrer">https://github.com/lukesampson/scoop<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a> <ul><li><a href="https://github.com/ScoopInstaller" target="_blank" rel="noopener noreferrer">https://github.com/ScoopInstaller<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></li> <li><a href="https://github.com/lukesampson/scoop-extras" target="_blank" rel="noopener noreferrer">https://github.com/lukesampson/scoop-extras<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></li></ul></li> <li><a href="https://github.com/cmderdev/cmder" target="_blank" rel="noopener noreferrer">https://github.com/cmderdev/cmder<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></li></ul> <h2 id="在batch中嵌入运行"><a href="#在batch中嵌入运行" class="header-anchor">#</a> 在batch中嵌入运行</h2> <blockquote><p>由于Power Shell默认没有开启运行脚本策略，可以以此方式解决</p></blockquote> <blockquote><p>正则表达式排除以<code>@</code>和<code>:</code>开头的行，并将其他所有内容传递给Power Shell</p></blockquote> <div class="language-batch extra-class"><pre class="language-batch"><code><span class="token operator">@</span><span class="token command"><span class="token keyword">findstr</span> <span class="token parameter attr-name">/v</span> <span class="token string">&quot;^@.* ^:.*&quot;</span> <span class="token string">&quot;%~f0&quot;</span>|powershell -</span><span class="token operator">&amp;</span><span class="token command"><span class="token keyword">goto</span><span class="token label property">:eof</span></span>
&lt;# 从这里开始是 Power Shell代码 #&gt;
</code></pre></div><blockquote><p>这里巧妙的借用Power Shell的注释把bat脚本命令包裹，把整个脚本内容传入Power Shell并执行</p></blockquote> <div class="language-batch extra-class"><pre class="language-batch"><code>&lt;# ::
<span class="token operator">@</span><span class="token command"><span class="token keyword">powershell</span> -&lt;<span class="token variable">%~f0</span> </span><span class="token operator">&amp;</span><span class="token command"><span class="token keyword">goto</span><span class="token label property">:eof</span></span>
#&gt;
# 从这里开始是 Power Shell代码
</code></pre></div><h2 id="windows10自带应用"><a href="#windows10自带应用" class="header-anchor">#</a> Windows10自带应用</h2> <blockquote><p>注意空格和英文标点</p></blockquote> <ul><li>查看已安装应用</li></ul> <div class="language-powershell extra-class"><pre class="language-powershell"><code><span class="token function">Get-AppxPackage</span> <span class="token punctuation">|</span> <span class="token function">Select</span> Name<span class="token punctuation">,</span>PackageFullName
</code></pre></div><ul><li>卸载应用程序</li></ul> <div class="language-powershell extra-class"><pre class="language-powershell"><code><span class="token comment"># 卸载所有账户中的应用</span>
<span class="token function">Get-AppxPackage</span> <span class="token operator">-</span>AllUsers <span class="token punctuation">|</span> <span class="token function">Remove-AppxPackage</span>
<span class="token comment"># 从系统账户中卸载应用</span>
<span class="token function">Get-AppXProvisionedPackage</span> <span class="token operator">-</span>online <span class="token punctuation">|</span> <span class="token function">Remove-AppxProvisionedPackage</span> –online
<span class="token comment"># 应用商店</span>
<span class="token function">get-appxpackage</span> <span class="token operator">*</span>store<span class="token operator">*</span> <span class="token punctuation">|</span> <span class="token function">remove-Appxpackage</span>
<span class="token comment"># 日历、邮件</span>
<span class="token function">Get-AppxPackage</span> Microsoft<span class="token punctuation">.</span>windowscommunicationsapps <span class="token punctuation">|</span> <span class="token function">Remove-AppxPackage</span>
<span class="token function">get-appxpackage</span> <span class="token operator">*</span>communicationsapps<span class="token operator">*</span> <span class="token punctuation">|</span> <span class="token function">remove-appxpackage</span>
<span class="token comment"># 从系统账户中卸载日历、邮件应用</span>
<span class="token function">Get-AppXProvisionedPackage</span> –online <span class="token function">where-object</span> <span class="token punctuation">{</span><span class="token variable">$_</span><span class="token punctuation">.</span>packagename –like <span class="token string">&quot;*windowsmunicationsapps*&quot;</span><span class="token punctuation">}</span> <span class="token punctuation">|</span> <span class="token function">remove-appxprovisionedpackage</span> –online
<span class="token comment"># 人脉</span>
<span class="token function">get-appxpackage</span> <span class="token operator">*</span>people<span class="token operator">*</span> <span class="token punctuation">|</span> <span class="token function">remove-appxpackage</span>
<span class="token comment"># Groove 音乐</span>
<span class="token function">get-appxpackage</span> <span class="token operator">*</span>zunemusic<span class="token operator">*</span> <span class="token punctuation">|</span> <span class="token function">remove-appxpackage</span>
<span class="token comment"># 电影和电视</span>
<span class="token function">get-appxpackage</span> <span class="token operator">*</span>zunevideo<span class="token operator">*</span> <span class="token punctuation">|</span> <span class="token function">remove-appxpackage</span>
<span class="token comment"># 财经</span>
<span class="token function">get-appxpackage</span> <span class="token operator">*</span>bingfinance<span class="token operator">*</span> <span class="token punctuation">|</span> <span class="token function">remove-appxpackage</span>
<span class="token comment"># 资讯</span>
<span class="token function">get-appxpackage</span> <span class="token operator">*</span>bingnews<span class="token operator">*</span> <span class="token punctuation">|</span> <span class="token function">remove-appxpackage</span>
<span class="token comment"># 体育</span>
<span class="token function">get-appxpackage</span> <span class="token operator">*</span>bingsports<span class="token operator">*</span> <span class="token punctuation">|</span> <span class="token function">remove-appxpackage</span>
<span class="token comment"># 天气</span>
<span class="token function">get-appxpackage</span> <span class="token operator">*</span>bingweather<span class="token operator">*</span> <span class="token punctuation">|</span> <span class="token function">remove-appxpackage</span>
<span class="token comment"># OneNote</span>
<span class="token function">get-appxpackage</span> <span class="token operator">*</span>onenote<span class="token operator">*</span> <span class="token punctuation">|</span> <span class="token function">remove-appxpackage</span>
<span class="token comment"># 闹钟和时钟</span>
<span class="token function">get-appxpackage</span> <span class="token operator">*</span>alarms<span class="token operator">*</span> <span class="token punctuation">|</span> <span class="token function">remove-appxpackage</span>
<span class="token comment"># 计算器</span>
<span class="token function">get-appxpackage</span> <span class="token operator">*</span>calculator<span class="token operator">*</span> <span class="token punctuation">|</span> <span class="token function">remove-appxpackage</span>
<span class="token comment"># 相机</span>
<span class="token function">get-appxpackage</span> <span class="token operator">*</span>camera<span class="token operator">*</span> <span class="token punctuation">|</span> <span class="token function">remove-appxpackage</span>
<span class="token comment"># 照片</span>
<span class="token function">get-appxpackage</span> <span class="token operator">*</span>photos<span class="token operator">*</span> <span class="token punctuation">|</span> <span class="token function">remove-appxpackage</span>
<span class="token comment"># 地图</span>
<span class="token function">get-appxpackage</span> <span class="token operator">*</span>maps<span class="token operator">*</span> <span class="token punctuation">|</span> <span class="token function">remove-appxpackage</span>
<span class="token comment"># 录音机</span>
<span class="token function">get-appxpackage</span> <span class="token operator">*</span>soundrecorder<span class="token operator">*</span> <span class="token punctuation">|</span> <span class="token function">remove-appxpackage</span>
<span class="token comment"># XBox</span>
<span class="token function">get-appxpackage</span> <span class="token operator">*</span>xbox<span class="token operator">*</span> <span class="token punctuation">|</span> <span class="token function">remove-appxpackage</span>
</code></pre></div><ul><li>恢复应用程序</li></ul> <div class="language-powershell extra-class"><pre class="language-powershell"><code><span class="token comment"># 应用商店</span>
<span class="token function">add-appxpackage</span> <span class="token operator">-</span>register <span class="token string">&quot;C:\Program Files\WindowsApps\*Store*\AppxManifest.xml&quot;</span> <span class="token operator">-</span>disabledevelopmentmode
<span class="token comment"># 计算器</span>
<span class="token function">Get-AppxPackage</span> <span class="token operator">*</span>calculator<span class="token operator">*</span> <span class="token operator">-</span>AllUsers<span class="token punctuation">|</span> <span class="token keyword">Foreach</span> <span class="token punctuation">{</span><span class="token function">Add-AppxPackage</span> <span class="token operator">-</span>DisableDevelopmentMode <span class="token operator">-</span>Register <span class="token string">&quot;<span class="token function">$<span class="token punctuation">(</span><span class="token variable">$_</span><span class="token punctuation">.</span>InstallLocation<span class="token punctuation">)</span></span>\AppXManifest.xml&quot;</span><span class="token punctuation">}</span>
<span class="token comment"># 日历、邮件</span>
<span class="token function">Get-AppxPackage</span> Microsoft<span class="token punctuation">.</span>windowscommunicationsapps <span class="token operator">-</span>AllUsers<span class="token punctuation">|</span> <span class="token keyword">Foreach</span> <span class="token punctuation">{</span><span class="token function">Add-AppxPackage</span> <span class="token operator">-</span>DisableDevelopmentMode <span class="token operator">-</span>Register <span class="token string">&quot;<span class="token function">$<span class="token punctuation">(</span><span class="token variable">$_</span><span class="token punctuation">.</span>InstallLocation<span class="token punctuation">)</span></span>\AppXManifest.xml&quot;</span><span class="token punctuation">}</span>
</code></pre></div><h2 id="命令"><a href="#命令" class="header-anchor">#</a> 命令</h2> <blockquote><p><code>ASCII</code> 为无BOM<code>UTF-8</code>编码</p></blockquote> <div class="language-powershell extra-class"><pre class="language-powershell"><code><span class="token comment"># 列出所有的环境变量</span>
<span class="token function">Get-ChildItem</span> env: 
<span class="token function">dir</span> env:
<span class="token function">ls</span> env:
<span class="token comment"># 获取环境变量的值</span>
<span class="token variable">$env</span>:变量名
<span class="token comment"># 删除环境变量</span>
<span class="token function">del</span> env:变量名
<span class="token comment"># 更新环境变量</span>
<span class="token variable">$env</span>:变量名=<span class="token string">&quot;变量值&quot;</span>
<span class="token comment"># .NET方法操作可以全局生效</span>
<span class="token namespace">[environment]</span>::SetEnvironmentvariable<span class="token punctuation">(</span><span class="token string">&quot;变量名&quot;</span><span class="token punctuation">,</span> <span class="token string">&quot;值&quot;</span><span class="token punctuation">,</span> <span class="token string">&quot;值&quot;</span><span class="token punctuation">)</span>

<span class="token comment"># 查看输出的命令集</span>
<span class="token function">Get-Command</span> <span class="token operator">-</span>verb out

<span class="token function">Export-Csv</span>
<span class="token function">Export-CliXML</span>
<span class="token function">Out-file</span>
<span class="token function">Out-GridView</span>
<span class="token function">ConvertTo-HTML</span> <span class="token punctuation">|</span> <span class="token function">Out-file</span>
</code></pre></div><ul><li>查看版本</li></ul> <div class="language-powershell extra-class"><pre class="language-powershell"><code><span class="token comment"># 查看Power Shell版本</span>
<span class="token variable">$PSVersionTable</span><span class="token punctuation">.</span>PSVersion
<span class="token variable">$host</span><span class="token punctuation">.</span>Version<span class="token punctuation">.</span>Major

<span class="token comment"># 查看当前PowerShell的.Net运行版本</span>
<span class="token variable">$PSVersionTable</span><span class="token punctuation">.</span>CLRVersion
<span class="token comment"># 查看所有安装的.Net 版本</span>
<span class="token function">dir</span> <span class="token string">'HKLM:\SOFTWARE\Microsoft\NET Framework Setup\NDP'</span> <span class="token punctuation">|</span> <span class="token function">sort-object</span> name <span class="token operator">-</span>Descending <span class="token punctuation">|</span> <span class="token function">select-object</span> <span class="token operator">-</span>ExpandProperty PSChildName

<span class="token comment"># 查看安装的.Net 客户端版本</span>
<span class="token punctuation">(</span><span class="token function">Get-ItemProperty</span> <span class="token string">'HKLM:\SOFTWARE\Microsoft\NET Framework Setup\NDP\v4\Client'</span> <span class="token operator">-</span>Name Version<span class="token punctuation">)</span><span class="token punctuation">.</span>Version
</code></pre></div><ul><li>为本账户启用执行脚本策略</li></ul> <div class="language-powershell extra-class"><pre class="language-powershell"><code><span class="token function">Set-ExecutionPolicy</span> <span class="token operator">-</span>ExecutionPolicy RemoteSigned <span class="token operator">-</span>Scope CurrentUser
</code></pre></div><ul><li>下载代码并通过<code>Invoke-Expression</code>执行</li></ul> <div class="language-powershell extra-class"><pre class="language-powershell"><code><span class="token function">Invoke-Expression</span> <span class="token punctuation">(</span><span class="token function">New-Object</span> System<span class="token punctuation">.</span>Net<span class="token punctuation">.</span>WebClient<span class="token punctuation">)</span><span class="token punctuation">.</span>DownloadString<span class="token punctuation">(</span><span class="token string">'https://get.scoop.sh'</span><span class="token punctuation">)</span>
</code></pre></div><ul><li>下载文件</li></ul> <div class="language-powershell extra-class"><pre class="language-powershell"><code><span class="token punctuation">(</span><span class="token function">New-Object</span> System<span class="token punctuation">.</span>Net<span class="token punctuation">.</span>WebClient<span class="token punctuation">)</span><span class="token punctuation">.</span>DownloadFile<span class="token punctuation">(</span><span class="token string">'https://git.io/JvYAg'</span><span class="token punctuation">,</span><span class="token string">'d:\\7za.exe'</span><span class="token punctuation">)</span>
</code></pre></div><ul><li>选择文件夹弹窗</li></ul> <div class="language-powershell extra-class"><pre class="language-powershell"><code><span class="token keyword">function</span> <span class="token function">Select-FolderDialog</span><span class="token punctuation">{</span>
    <span class="token keyword">param</span><span class="token punctuation">(</span><span class="token namespace">[string]</span><span class="token variable">$Directory</span><span class="token punctuation">,</span><span class="token namespace">[string]</span><span class="token variable">$Description</span><span class="token punctuation">,</span><span class="token namespace">[boolean]</span><span class="token variable">$ShowNewFolderButton</span><span class="token punctuation">)</span>
    <span class="token namespace">[System.Reflection.Assembly]</span>::LoadWithPartialName<span class="token punctuation">(</span><span class="token string">&quot;System.Windows.Forms&quot;</span><span class="token punctuation">)</span> <span class="token punctuation">|</span> <span class="token function">Out-Null</span>
    <span class="token variable">$objForm</span> = <span class="token function">New-Object</span> System<span class="token punctuation">.</span>Windows<span class="token punctuation">.</span>Forms<span class="token punctuation">.</span>FolderBrowserDialog
    <span class="token variable">$objForm</span><span class="token punctuation">.</span>RootFolder = <span class="token variable">$Directory</span>
    <span class="token variable">$objForm</span><span class="token punctuation">.</span>Description = <span class="token variable">$Description</span>
    <span class="token variable">$objForm</span><span class="token punctuation">.</span>ShowNewFolderButton = <span class="token variable">$ShowNewFolderButton</span>
    <span class="token variable">$Show</span> = <span class="token variable">$objForm</span><span class="token punctuation">.</span>ShowDialog<span class="token punctuation">(</span><span class="token punctuation">)</span>
    <span class="token keyword">If</span> <span class="token punctuation">(</span><span class="token variable">$Show</span> <span class="token operator">-eq</span> <span class="token string">&quot;OK&quot;</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">Return</span> <span class="token variable">$objForm</span><span class="token punctuation">.</span>SelectedPath
    <span class="token punctuation">}</span> <span class="token keyword">Else</span> <span class="token punctuation">{</span>
        <span class="token comment"># 输出错误信息</span>
        <span class="token function">Write-Error</span> <span class="token string">&quot;error information here&quot;</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre></div><ul><li>循环目录</li></ul> <div class="language-powershell extra-class"><pre class="language-powershell"><code><span class="token comment"># 获取子目录和文件并遍历</span>
<span class="token function">Get-ChildItem</span> <span class="token punctuation">.</span> <span class="token punctuation">|</span> <span class="token function">ForEach-Object</span> <span class="token operator">-</span><span class="token keyword">Process</span> <span class="token punctuation">{</span>
    <span class="token variable">$project_name</span> = <span class="token variable">$_</span><span class="token punctuation">.</span>Name
    <span class="token comment"># 如果为子目录</span>
    <span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token variable">$_</span> <span class="token operator">-is</span> <span class="token namespace">[System.IO.DirectoryInfo]</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token comment"># 切换到子目录</span>
        <span class="token function">Set-location</span> <span class="token variable">$_</span><span class="token punctuation">.</span>FullName
        <span class="token comment"># 判断目录是否存在</span>
        <span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token function">Test-Path</span> <span class="token punctuation">.</span>git<span class="token punctuation">)</span> <span class="token punctuation">{</span>
            git remote <span class="token operator">-</span>v
            <span class="token function">Write-Host</span><span class="token punctuation">(</span><span class="token string">&quot;&quot;</span><span class="token punctuation">.</span>PadLeft<span class="token punctuation">(</span>15<span class="token punctuation">,</span> <span class="token string">&quot;*&quot;</span><span class="token punctuation">)</span> <span class="token operator">+</span> <span class="token string">&quot;开始更新 <span class="token variable">$project_name</span> &quot;</span><span class="token punctuation">.</span>PadRight<span class="token punctuation">(</span>30<span class="token punctuation">,</span> <span class="token string">&quot;*&quot;</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
            git pull
        <span class="token punctuation">}</span>
        <span class="token function">Set-Location</span> <span class="token punctuation">.</span><span class="token punctuation">.</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre></div><ul><li>创建文件夹</li></ul> <div class="language-powershell extra-class"><pre class="language-powershell"><code><span class="token function">New-Item</span> <span class="token operator">-</span>ItemType directory <span class="token operator">-</span>Path 目录的路径
<span class="token comment"># 只列出目录</span>
<span class="token function">Dir</span> <span class="token punctuation">|</span> <span class="token function">Where-Object</span> <span class="token punctuation">{</span> <span class="token variable">$_</span> <span class="token operator">-is</span> <span class="token namespace">[System.IO.DirectoryInfo]</span> <span class="token punctuation">}</span>
<span class="token function">Dir</span> <span class="token punctuation">|</span> <span class="token function">Where-Object</span> <span class="token punctuation">{</span> <span class="token variable">$_</span><span class="token punctuation">.</span>PSIsContainer <span class="token punctuation">}</span>
<span class="token function">Dir</span> <span class="token punctuation">|</span> <span class="token function">Where-Object</span> <span class="token punctuation">{</span> <span class="token variable">$_</span><span class="token punctuation">.</span>Mode<span class="token punctuation">.</span>Substring<span class="token punctuation">(</span>0<span class="token punctuation">,</span>1<span class="token punctuation">)</span> <span class="token operator">-eq</span> <span class="token string">&quot;d&quot;</span> <span class="token punctuation">}</span>
<span class="token comment"># 只列出文件</span>
<span class="token function">Dir</span> <span class="token punctuation">|</span> <span class="token function">Where-Object</span> <span class="token punctuation">{</span> <span class="token variable">$_</span> <span class="token operator">-is</span> <span class="token namespace">[System.IO.FileInfo]</span> <span class="token punctuation">}</span>
<span class="token function">Dir</span> <span class="token punctuation">|</span> <span class="token function">Where-Object</span> <span class="token punctuation">{</span> <span class="token variable">$_</span><span class="token punctuation">.</span>PSIsContainer <span class="token operator">-eq</span> <span class="token boolean">$false</span><span class="token punctuation">}</span>
<span class="token function">Dir</span> <span class="token punctuation">|</span> <span class="token function">Where-Object</span> <span class="token punctuation">{</span> <span class="token variable">$_</span><span class="token punctuation">.</span>Mode<span class="token punctuation">.</span>Substring<span class="token punctuation">(</span>0<span class="token punctuation">,</span>1<span class="token punctuation">)</span> <span class="token operator">-ne</span> <span class="token string">&quot;d&quot;</span> <span class="token punctuation">}</span>
<span class="token comment"># 通过管道过滤2020年5月12日后更改过的文件</span>
<span class="token function">Dir</span> <span class="token punctuation">|</span> <span class="token function">Where-Object</span> <span class="token punctuation">{</span> <span class="token variable">$_</span><span class="token punctuation">.</span>CreationTime <span class="token operator">-gt</span> <span class="token namespace">[datetime]</span>::Parse<span class="token punctuation">(</span><span class="token string">&quot;May 12, 2020&quot;</span><span class="token punctuation">)</span> <span class="token punctuation">}</span>
<span class="token comment"># 获取2周以内更改过的文件</span>
<span class="token function">Dir</span> <span class="token punctuation">|</span> <span class="token function">Where-Object</span> <span class="token punctuation">{</span> <span class="token variable">$_</span><span class="token punctuation">.</span>CreationTime <span class="token operator">-gt</span> <span class="token punctuation">(</span><span class="token function">Get-Date</span><span class="token punctuation">)</span><span class="token punctuation">.</span>AddDays<span class="token punctuation">(</span><span class="token operator">-</span>14<span class="token punctuation">)</span> <span class="token punctuation">}</span>
</code></pre></div><ul><li>复制文件及目录结构</li></ul> <div class="language-powershell extra-class"><pre class="language-powershell"><code><span class="token comment"># 源路径最后一个文件夹名为需要复制的目录结构的顶级文件夹名</span>
<span class="token function">Copy-Item</span> <span class="token operator">-</span>Force <span class="token operator">-</span>Recurse 源路径 目标路径 <span class="token operator">-</span><span class="token keyword">Filter</span> 文件名
<span class="token comment"># .create()方法可以创建不存在的目录</span>
<span class="token namespace">[IO.DirectoryInfo]</span><span class="token variable">$to</span> <span class="token punctuation">|</span><span class="token operator">%</span> <span class="token punctuation">{</span><span class="token variable">$_</span><span class="token punctuation">.</span>create<span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token function">cp</span> <span class="token variable">$from</span> <span class="token variable">$_</span><span class="token punctuation">}</span>
<span class="token comment"># 复制多个文件到指定目录</span>
<span class="token function">get-childitem</span> <span class="token operator">-</span>path <span class="token string">&quot;D:\demo&quot;</span> <span class="token operator">-</span>include @<span class="token punctuation">(</span><span class="token string">'test.java'</span><span class="token punctuation">,</span><span class="token string">'test.js'</span><span class="token punctuation">)</span> <span class="token operator">-</span>recurse `
<span class="token punctuation">|</span> <span class="token function">copy-item</span> <span class="token operator">-</span>destination <span class="token punctuation">(</span><span class="token function">new-item</span> <span class="token operator">-</span>path <span class="token string">&quot;<span class="token variable">$env</span>:HOMEPATH\Desktop\demo&quot;</span> <span class="token operator">-</span><span class="token function">type</span> <span class="token string">&quot;directory&quot;</span><span class="token punctuation">)</span>
</code></pre></div><ul><li>删除空目录</li></ul> <div class="language-powershell extra-class"><pre class="language-powershell"><code><span class="token function">Get-ChildItem</span> <span class="token operator">-</span>Path 路径 <span class="token operator">-</span>Recurse <span class="token operator">-</span>Force `
<span class="token punctuation">|</span> <span class="token function">Where-Object</span> <span class="token punctuation">{</span> <span class="token variable">$_</span><span class="token punctuation">.</span>PSIsContainer <span class="token operator">-and</span> `
    <span class="token punctuation">(</span><span class="token function">Get-ChildItem</span> <span class="token operator">-</span>Path <span class="token variable">$_</span><span class="token punctuation">.</span>FullName <span class="token operator">-</span>Recurse <span class="token operator">-</span>Force `
    <span class="token punctuation">|</span> <span class="token function">Where-Object</span> <span class="token punctuation">{</span> <span class="token operator">!</span><span class="token variable">$_</span><span class="token punctuation">.</span>PSIsContainer <span class="token punctuation">}</span><span class="token punctuation">)</span> <span class="token operator">-eq</span> <span class="token variable">$null</span> <span class="token punctuation">}</span> `
<span class="token punctuation">|</span> <span class="token function">Remove-Item</span> <span class="token operator">-</span>Force <span class="token operator">-</span>Recurse

<span class="token function">Get-ChildItem</span> <span class="token operator">-</span>recurse <span class="token punctuation">|</span> Where <span class="token punctuation">{</span><span class="token variable">$_</span><span class="token punctuation">.</span>PSIsContainer <span class="token operator">-and</span> `
@<span class="token punctuation">(</span><span class="token function">Get-ChildItem</span> <span class="token operator">-</span>Lit <span class="token variable">$_</span><span class="token punctuation">.</span>Fullname <span class="token operator">-</span>r <span class="token punctuation">|</span> Where <span class="token punctuation">{</span><span class="token operator">!</span><span class="token variable">$_</span><span class="token punctuation">.</span>PSIsContainer<span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">.</span>Length <span class="token operator">-eq</span> 0<span class="token punctuation">}</span> `
<span class="token punctuation">|</span> <span class="token function">Remove-Item</span> <span class="token operator">-</span>recurse <span class="token operator">-</span>whatif

<span class="token function">get-childitem</span> <span class="token operator">-</span>recurse <span class="token punctuation">|</span> ? <span class="token punctuation">{</span><span class="token variable">$_</span><span class="token punctuation">.</span>GetType<span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">-match</span><span class="token string">&quot;DirectoryInfo&quot;</span><span class="token punctuation">}</span> `
<span class="token punctuation">|</span> ?<span class="token punctuation">{</span> <span class="token variable">$_</span><span class="token punctuation">.</span>GetFiles<span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span>Count <span class="token operator">-eq</span> 0 <span class="token operator">-and</span> <span class="token variable">$_</span><span class="token punctuation">.</span>GetDirectories<span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span>Count <span class="token operator">-eq</span> 0 <span class="token punctuation">}</span> <span class="token punctuation">|</span> <span class="token function">rm</span> <span class="token operator">-</span>whatif

<span class="token function">ls</span> <span class="token operator">-</span>recurse <span class="token punctuation">|</span> where <span class="token punctuation">{</span><span class="token operator">!</span>@<span class="token punctuation">(</span><span class="token function">ls</span> <span class="token operator">-</span>force <span class="token variable">$_</span><span class="token punctuation">.</span>fullname<span class="token punctuation">)</span><span class="token punctuation">}</span> <span class="token punctuation">|</span> <span class="token function">rm</span> <span class="token operator">-</span>whatif <span class="token operator">-</span>Force
</code></pre></div><ul><li>递归删除排除之外的文件</li></ul> <div class="language-powershell extra-class"><pre class="language-powershell"><code><span class="token function">dir</span> <span class="token operator">-</span>Path 源路径 <span class="token operator">-</span>Include <span class="token operator">*</span><span class="token punctuation">.</span><span class="token operator">*</span> <span class="token operator">-</span>Exclude @<span class="token punctuation">(</span><span class="token string">'test.js'</span><span class="token punctuation">,</span><span class="token string">'test.css'</span><span class="token punctuation">,</span><span class="token string">'a*'</span><span class="token punctuation">)</span> <span class="token operator">-</span>Recurse <span class="token punctuation">|</span> <span class="token function">del</span> <span class="token operator">-</span>Recurse
</code></pre></div><ul><li>删除旧文件（指定日期之前的）</li></ul> <div class="language-powershell extra-class"><pre class="language-powershell"><code><span class="token function">Get-ChildItem</span> <span class="token operator">-</span>recurse <span class="token punctuation">|</span> Where <span class="token punctuation">{</span><span class="token operator">!</span><span class="token variable">$_</span><span class="token punctuation">.</span>PSIsContainer <span class="token operator">-and</span> `
<span class="token variable">$_</span><span class="token punctuation">.</span>LastWriteTime <span class="token operator">-lt</span> <span class="token punctuation">(</span><span class="token function">get-date</span><span class="token punctuation">)</span><span class="token punctuation">.</span>AddDays<span class="token punctuation">(</span><span class="token operator">-</span>31<span class="token punctuation">)</span><span class="token punctuation">}</span> <span class="token punctuation">|</span> <span class="token function">Remove-Item</span> <span class="token operator">-</span>whatif

<span class="token function">Get-ChildItem</span> <span class="token operator">-</span>Path 路径 <span class="token operator">-</span>Recurse <span class="token operator">-</span>Force `
<span class="token punctuation">|</span> <span class="token function">Where-Object</span> <span class="token punctuation">{</span> <span class="token operator">!</span><span class="token variable">$_</span><span class="token punctuation">.</span>PSIsContainer <span class="token operator">-and</span> <span class="token variable">$_</span><span class="token punctuation">.</span>CreationTime <span class="token operator">-lt</span> <span class="token punctuation">(</span><span class="token function">Get-Date</span><span class="token punctuation">)</span><span class="token punctuation">.</span>AddDays<span class="token punctuation">(</span><span class="token operator">-</span>15<span class="token punctuation">)</span> <span class="token punctuation">}</span> `
<span class="token punctuation">|</span> <span class="token function">Remove-Item</span> <span class="token operator">-</span>Force

<span class="token function">get-childitem</span> <span class="token operator">-</span>recurse <span class="token punctuation">|</span> ? <span class="token punctuation">{</span><span class="token variable">$_</span><span class="token punctuation">.</span>GetType<span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">-match</span><span class="token string">&quot;FileInfo&quot;</span><span class="token punctuation">}</span> `
<span class="token punctuation">|</span> ?<span class="token punctuation">{</span> <span class="token variable">$_</span><span class="token punctuation">.</span>LastWriteTime <span class="token operator">-lt</span> <span class="token namespace">[datetime]</span>::now<span class="token punctuation">.</span>adddays<span class="token punctuation">(</span><span class="token operator">-</span>30<span class="token punctuation">)</span> <span class="token punctuation">}</span> <span class="token punctuation">|</span> <span class="token function">rm</span> <span class="token operator">-</span>whatif <span class="token operator">-</span>Force
</code></pre></div><ul><li>格式化时间</li></ul> <div class="language-powershell extra-class"><pre class="language-powershell"><code><span class="token function">Get-Date</span> <span class="token operator">-</span>Format <span class="token string">'yyy-MM-dd hh:mm:ss'</span>
</code></pre></div><ul><li>下载安装7z</li></ul> <div class="language-powershell extra-class"><pre class="language-powershell"><code><span class="token variable">$response</span> = <span class="token function">Invoke-WebRequest</span> <span class="token operator">-</span>Uri https:<span class="token operator">/</span><span class="token operator">/</span>www<span class="token punctuation">.</span>7<span class="token operator">-</span>zip<span class="token punctuation">.</span>org<span class="token operator">/</span>download<span class="token punctuation">.</span>html
<span class="token comment"># 获取文件名</span>
<span class="token variable">$match</span> = <span class="token variable">$response</span><span class="token punctuation">.</span>Content <span class="token punctuation">|</span> <span class="token function">Select-String</span> <span class="token operator">-</span>Pattern <span class="token string">'&lt;A href=&quot;(?&lt;url&gt;a\/7z\d+-x64\.msi)&quot;&gt;Download&lt;\/A&gt;'</span>
<span class="token comment"># 拼接下载url</span>
<span class="token variable">$url</span> = <span class="token string">&quot;https://www.7-zip.org/&quot;</span> <span class="token operator">+</span> <span class="token variable">$match</span><span class="token punctuation">.</span>Matches<span class="token punctuation">[</span>0<span class="token punctuation">]</span><span class="token punctuation">.</span>Groups<span class="token punctuation">[</span><span class="token string">'url'</span><span class="token punctuation">]</span><span class="token punctuation">.</span>Value
<span class="token comment"># 请求下载</span>
<span class="token function">Invoke-WebRequest</span> <span class="token operator">-</span>Uri <span class="token variable">$url</span> <span class="token operator">-</span>OutFile 7zip<span class="token punctuation">.</span>msi
<span class="token comment"># 使用msiexec解压msi到7zip目录</span>
<span class="token variable">$process</span> = <span class="token function">Start-Process</span> msiexec <span class="token operator">-</span>ArgumentList <span class="token string">&quot;/a 7zip.msi /qn TARGETDIR=`&quot;<span class="token function">$<span class="token punctuation">(</span><span class="token function">Get-Location</span><span class="token punctuation">)</span></span>\7zip`&quot;&quot;</span> <span class="token operator">-</span>PassThru
<span class="token function">Wait-Process</span> <span class="token operator">-</span>Id <span class="token variable">$process</span><span class="token punctuation">.</span>id
<span class="token function">Move-Item</span> 7zip<span class="token operator">/</span>Files<span class="token operator">/</span>7<span class="token operator">-</span>Zip<span class="token operator">/</span>7z<span class="token punctuation">.</span>exe 7z<span class="token punctuation">.</span>exe <span class="token operator">-</span>Force
<span class="token function">Move-Item</span> 7zip<span class="token operator">/</span>Files<span class="token operator">/</span>7<span class="token operator">-</span>Zip<span class="token operator">/</span>7z<span class="token punctuation">.</span>dll 7z<span class="token punctuation">.</span>dll <span class="token operator">-</span>Force
<span class="token function">Remove-Item</span> –path 7zip –Recurse
<span class="token function">Remove-Item</span> –path 7zip<span class="token punctuation">.</span>msi
</code></pre></div><p><strong>调用DLL</strong></p> <div class="language-powershell extra-class"><pre class="language-powershell"><code><span class="token punctuation">[</span>DllImport<span class="token punctuation">(</span><span class="token string">&quot;kernel32.dll&quot;</span><span class="token punctuation">)</span><span class="token punctuation">]</span>
private static extern int GetPrivateProfileSection<span class="token punctuation">(</span>string lpAppName<span class="token punctuation">,</span> byte<span class="token punctuation">[</span><span class="token punctuation">]</span> lpszReturnBuffer<span class="token punctuation">,</span> int nSize<span class="token punctuation">,</span> string lpFileName<span class="token punctuation">)</span><span class="token punctuation">;</span>

<span class="token keyword">Function</span> <span class="token function">Lock-WorkStation</span> <span class="token punctuation">{</span>
    <span class="token variable">$signature</span> = @<span class="token string">&quot;
        [DllImport(&quot;</span>user32<span class="token punctuation">.</span>dll<span class="token string">&quot;, SetLastError = true)]
        public static extern bool LockWorkStation();
    &quot;</span>@
    
    <span class="token variable">$LockWorkStation</span> = <span class="token function">Add-Type</span> <span class="token operator">-</span>memberDefinition <span class="token variable">$signature</span> <span class="token operator">-</span>name <span class="token string">&quot;Win32LockWorkStation&quot;</span> <span class="token operator">-</span>namespace Win32Functions <span class="token operator">-</span>passthru
    <span class="token variable">$LockWorkStation</span>::LockWorkStation<span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">|</span> <span class="token function">Out-Null</span>
<span class="token punctuation">}</span>
</code></pre></div><p><strong>查看进程启动参数</strong></p> <div class="language-powershell extra-class"><pre class="language-powershell"><code><span class="token comment"># 查看所有进程</span>
wmic <span class="token keyword">process</span> get caption<span class="token punctuation">,</span>commandline <span class="token operator">/</span>value
<span class="token comment"># 查看单个进程</span>
wmic <span class="token keyword">process</span> where caption=<span class="token string">&quot;svchost.exe&quot;</span> get caption<span class="token punctuation">,</span>commandline <span class="token operator">/</span>value
wmic <span class="token keyword">process</span> get caption<span class="token punctuation">,</span>commandline <span class="token operator">/</span>value <span class="token punctuation">|</span> findstr <span class="token string">&quot;svchost.exe&quot;</span>
</code></pre></div><p><strong>解压zip</strong></p> <ul><li><a href="https://docs.microsoft.com/zh-cn/powershell/module/microsoft.powershell.archive" target="_blank" rel="noopener noreferrer">压缩解压文件<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></li> <li><a href="https://blog.csdn.net/ahxdyz/article/details/93534213" target="_blank" rel="noopener noreferrer">PowerShell-压缩解压缩文件<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></li></ul> <ul><li>PowerShell 5.0或更高版本(已预安装Windows 10和Windows Server 2016)</li></ul> <div class="language-powershell extra-class"><pre class="language-powershell"><code><span class="token function">Compress-Archive</span> <span class="token operator">-</span>Path 解压到目录路径 <span class="token operator">-</span>DestinationPath 压缩文件路径
<span class="token function">Expand-Archive</span> <span class="token operator">-</span>Path 解压到目录路径 <span class="token operator">-</span>DestinationPath 压缩文件路径 <span class="token operator">-</span>Force:<span class="token variable">$Overwrite</span>
</code></pre></div><ul><li>需要.net 4.5以上</li></ul> <div class="language-powershell extra-class"><pre class="language-powershell"><code><span class="token function">Add-Type</span> <span class="token operator">-</span>assembly <span class="token string">&quot;system.io.compression.filesystem&quot;</span>
<span class="token namespace">[io.compression.zipfile]</span>::ExtractToDirectory<span class="token punctuation">(</span>压缩文件路径<span class="token punctuation">,</span> 解压到目录路径<span class="token punctuation">)</span>
</code></pre></div><ul><li>PowerShell 2.0以上</li></ul> <div class="language-powershell extra-class"><pre class="language-powershell"><code><span class="token keyword">function</span> UnzipFile<span class="token punctuation">(</span><span class="token namespace">[string]</span><span class="token variable">$sourceFile</span><span class="token punctuation">,</span> <span class="token namespace">[string]</span><span class="token variable">$targetFolder</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
    <span class="token keyword">if</span><span class="token punctuation">(</span><span class="token operator">!</span><span class="token punctuation">(</span><span class="token function">Test-Path</span> <span class="token variable">$targetFolder</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
        mkdir <span class="token variable">$targetFolder</span>
    <span class="token punctuation">}</span>
    <span class="token variable">$shellApp</span> = <span class="token function">New-Object</span> <span class="token operator">-</span>ComObject Shell<span class="token punctuation">.</span>Application
    <span class="token variable">$files</span> = <span class="token variable">$shellApp</span><span class="token punctuation">.</span>NameSpace<span class="token punctuation">(</span><span class="token variable">$souceFile</span><span class="token punctuation">)</span><span class="token punctuation">.</span>Items<span class="token punctuation">(</span><span class="token punctuation">)</span>
    <span class="token comment"># foreach ($item in $files) {</span>
    <span class="token comment">#     $shell.Namespace($targetFolder).CopyHere($item)</span>
    <span class="token comment"># }</span>
    <span class="token variable">$shellApp</span><span class="token punctuation">.</span>NameSpace<span class="token punctuation">(</span><span class="token variable">$targetFolder</span><span class="token punctuation">)</span><span class="token punctuation">.</span>CopyHere<span class="token punctuation">(</span><span class="token variable">$files</span><span class="token punctuation">)</span>
<span class="token punctuation">}</span>
</code></pre></div><h3 id="事件计划任务"><a href="#事件计划任务" class="header-anchor">#</a> 事件计划任务</h3> <ul><li><a href="https://www.holoem.com/?p=1974" target="_blank" rel="noopener noreferrer">Scheduled Tasks（计划任务）<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></li></ul> <p><strong>使用Get-WinEvent</strong></p> <ul><li><a href="https://docs.microsoft.com/zh-cn/powershell/module/microsoft.powershell.diagnostics/get-winevent" target="_blank" rel="noopener noreferrer">Get-WinEvent<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></li></ul> <div class="language-powershell extra-class"><pre class="language-powershell"><code><span class="token function">Get-Help</span> <span class="token function">Get-WinEvent</span>

<span class="token function">Get-WinEvent</span> @<span class="token punctuation">{</span>logname=<span class="token string">'application'</span><span class="token punctuation">,</span><span class="token string">'system'</span><span class="token punctuation">}</span> <span class="token operator">-</span>MaxEvents 1
<span class="token comment"># 列出所有事件日志</span>
<span class="token function">Get-WinEvent</span> <span class="token operator">-</span>ListLog <span class="token operator">*</span>
<span class="token comment"># powershell管理员权限下获取安全事件日志</span>
<span class="token function">Get-WinEvent</span> <span class="token operator">-</span>FilterHashtable @<span class="token punctuation">{</span>LogName=<span class="token string">'Security'</span><span class="token punctuation">}</span>
<span class="token comment"># 过滤安全事件ID 4624</span>
<span class="token function">Get-WinEvent</span> <span class="token operator">-</span>FilterHashtable @<span class="token punctuation">{</span>LogName=<span class="token string">'Security'</span><span class="token punctuation">;</span>ID=<span class="token string">'4624'</span><span class="token punctuation">}</span>
<span class="token comment"># 查询今天的应用和系统日志，显示前2条</span>
<span class="token function">Get-WinEvent</span> @<span class="token punctuation">{</span>logname=<span class="token string">'application'</span><span class="token punctuation">,</span><span class="token string">'system'</span><span class="token punctuation">;</span>starttime=<span class="token namespace">[datetime]</span>::today <span class="token punctuation">}</span> <span class="token operator">-</span>MaxEvents 2
<span class="token comment"># 根据ID查询事件</span>
<span class="token function">Get-WinEvent</span> <span class="token operator">-</span>LogName Microsoft<span class="token operator">-</span>Windows<span class="token operator">-</span>PowerShell<span class="token operator">/</span>Operational <span class="token punctuation">|</span> <span class="token function">Where-Object</span> <span class="token punctuation">{</span><span class="token variable">$_</span><span class="token punctuation">.</span>ID <span class="token operator">-eq</span> <span class="token string">&quot;4100&quot;</span> <span class="token operator">-or</span> <span class="token variable">$_</span><span class="token punctuation">.</span>ID <span class="token operator">-eq</span> <span class="token string">&quot;4104&quot;</span><span class="token punctuation">}</span>
<span class="token comment"># 查询指定时间内的事件</span>
<span class="token variable">$StartTime</span>=<span class="token function">Get-Date</span>  <span class="token operator">-</span>Year  2020  <span class="token operator">-</span>Month  3  <span class="token operator">-</span>Day  1  <span class="token operator">-</span>Hour  15  <span class="token operator">-</span>Minute  30
<span class="token variable">$EndTime</span>=<span class="token function">Get-Date</span>  <span class="token operator">-</span>Year  2020  <span class="token operator">-</span>Month  3  <span class="token operator">-</span>Day  15  <span class="token operator">-</span>Hour  20  <span class="token operator">-</span>Minute  00
<span class="token function">Get-WinEvent</span> <span class="token operator">-</span>FilterHashtable @<span class="token punctuation">{</span>LogName=<span class="token string">'System'</span><span class="token punctuation">;</span>StartTime=<span class="token variable">$StartTime</span><span class="token punctuation">;</span>EndTime=<span class="token variable">$EndTime</span><span class="token punctuation">}</span>
</code></pre></div><p><strong>使用Get-EventLog</strong></p> <ul><li><a href="https://docs.microsoft.com/zh-cn/powershell/module/microsoft.powershell.management/get-eventlog" target="_blank" rel="noopener noreferrer">Get-EventLog<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></li> <li><a href="https://www.pstips.net/monitoring-eventlog.html" target="_blank" rel="noopener noreferrer">PowerShell 监听事件日志<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></li> <li><a href="https://www.pstips.net/get-sleep-and-hibernation-times.html" target="_blank" rel="noopener noreferrer">Powershell获取睡眠时间<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></li></ul> <div class="language-powershell extra-class"><pre class="language-powershell"><code><span class="token function">Get-Help</span> <span class="token function">Get-EventLog</span>

<span class="token function">Get-EventLog</span> <span class="token operator">-</span>LogName system <span class="token operator">-</span>Source user32
<span class="token comment"># 按 EventID 将事件进行分组</span>
<span class="token function">Get-EventLog</span> <span class="token operator">-</span>LogName system <span class="token operator">-</span>Source user32 <span class="token punctuation">|</span> <span class="token function">group</span> EventID
<span class="token comment"># fl 是 Format-List 的别名</span>
<span class="token function">Get-EventLog</span> <span class="token operator">-</span>LogName system <span class="token operator">-</span>Source user32 <span class="token operator">-</span>Newest 1 <span class="token punctuation">|</span> <span class="token function">fl</span> <span class="token operator">*</span>
<span class="token function">Get-EventLog</span> <span class="token operator">-</span>LogName system <span class="token operator">-</span>Source user32 <span class="token punctuation">|</span> <span class="token function">Select</span> TimeGenerated<span class="token punctuation">,</span> Message
<span class="token function">Get-EventLog</span> <span class="token operator">-</span>LogName system <span class="token operator">-</span>Source user32 <span class="token punctuation">|</span> <span class="token function">Select</span> TimeGenerated<span class="token punctuation">,</span> Message <span class="token punctuation">|</span> <span class="token function">sort</span> message
<span class="token function">Get-EventLog</span> <span class="token operator">-</span>LogName system <span class="token operator">-</span>Source user32 <span class="token punctuation">|</span> <span class="token function">Select</span> TimeGenerated<span class="token punctuation">,</span> Message <span class="token punctuation">|</span> <span class="token function">sort</span> message <span class="token punctuation">|</span> <span class="token function">ft</span> <span class="token operator">-</span>Wrap
</code></pre></div><p><strong>定时任务</strong></p> <ul><li>查看支持的命令</li></ul> <div class="language-powershell extra-class"><pre class="language-powershell"><code><span class="token function">Get-Command</span> <span class="token operator">-</span>Module ScheduledTasks
</code></pre></div><div class="language-powershell extra-class"><pre class="language-powershell"><code><span class="token comment"># 此例子为每5分钟一次的定时任务，通过设置$step和$add可以实现延时执行任务。 </span>
<span class="token keyword">function</span> waitsec<span class="token punctuation">{</span>
    <span class="token variable">$step</span>=300 <span class="token comment">#设置间隔</span>
    <span class="token variable">$add</span>=0 <span class="token comment">#设置延时</span>
    <span class="token variable">$t</span>=<span class="token punctuation">(</span><span class="token function">get-date</span><span class="token punctuation">)</span>
    <span class="token variable">$step</span><span class="token operator">-</span><span class="token punctuation">(</span><span class="token punctuation">(</span><span class="token variable">$t</span><span class="token punctuation">.</span>Hour<span class="token operator">*</span>3600<span class="token operator">+</span><span class="token variable">$t</span><span class="token punctuation">.</span>Minute<span class="token operator">*</span>60<span class="token operator">+</span><span class="token variable">$t</span><span class="token punctuation">.</span>Second<span class="token punctuation">)</span><span class="token operator">%</span><span class="token variable">$step</span><span class="token punctuation">)</span><span class="token operator">+</span><span class="token variable">$add</span>
<span class="token punctuation">}</span>
 
<span class="token function">write-host</span> <span class="token string">&quot;running...... please wait&quot;</span> <span class="token punctuation">(</span>waitsec<span class="token punctuation">)</span><span class="token string">&quot;S&quot;</span> 
<span class="token function">Start-Sleep</span> <span class="token operator">-</span>s <span class="token punctuation">(</span>waitsec<span class="token punctuation">)</span>
<span class="token keyword">while</span><span class="token punctuation">(</span>1<span class="token punctuation">)</span><span class="token punctuation">{</span>
    <span class="token comment"># 执行代码</span>
    <span class="token function">get-date</span>
    <span class="token function">Start-Sleep</span> <span class="token operator">-</span>s <span class="token punctuation">(</span>waitsec<span class="token punctuation">)</span>
<span class="token punctuation">}</span>
</code></pre></div><h2 id="随机字符串"><a href="#随机字符串" class="header-anchor">#</a> 随机字符串</h2> <blockquote><p><code>48..57</code>是数字0-9，powershell的范围操作符是<code>..</code>，<code>65..90</code>是大写字符A到Z，<code>97..122</code>是小写字母。
如果需要获取多有的可打印字符（包括空格）的话，范围是<code>32..127</code>。</p></blockquote> <blockquote><p><code>[char]</code>把对应数字转换成字符，例如 <code>[char](66)</code>就是B(大写字母B)，C语言使用的小括号来进行类型强制转换。</p></blockquote> <div class="language-powershell extra-class"><pre class="language-powershell"><code><span class="token comment"># 如果不指定-count参数，则前面的list有多少个字符</span>
<span class="token comment"># get-random就会获取多少个字符，只是顺序打乱了</span>
<span class="token operator">-join</span><span class="token punctuation">(</span><span class="token punctuation">(</span>48<span class="token punctuation">.</span><span class="token punctuation">.</span>57 <span class="token operator">+</span> 65<span class="token punctuation">.</span><span class="token punctuation">.</span>90 <span class="token operator">+</span> 97<span class="token punctuation">.</span><span class="token punctuation">.</span>122<span class="token punctuation">)</span> <span class="token punctuation">|</span> <span class="token function">get-random</span> <span class="token operator">-</span>count 6 <span class="token punctuation">|</span> <span class="token operator">%</span><span class="token punctuation">{</span><span class="token namespace">[char]</span><span class="token variable">$_</span><span class="token punctuation">}</span><span class="token punctuation">)</span>

<span class="token comment"># 输出19位数字字符串并在开头拼接x</span>
<span class="token punctuation">(</span>0<span class="token punctuation">.</span><span class="token punctuation">.</span>13<span class="token punctuation">|</span><span class="token function">Get-Random</span> <span class="token operator">-</span>count 19<span class="token punctuation">)</span> <span class="token operator">-join</span> <span class="token variable">$null</span> <span class="token punctuation">|</span> <span class="token operator">%</span><span class="token punctuation">{</span><span class="token operator">-join</span> <span class="token punctuation">(</span><span class="token string">&quot;X&quot;</span><span class="token punctuation">,</span><span class="token variable">$_</span><span class="token punctuation">)</span><span class="token punctuation">}</span>

<span class="token comment"># 这里的0..1024相当于循环控制，每循环一次|后面的代码执行一次，</span>
<span class="token comment"># 其中在数字字母中随机选一个字符 0..1024, like Perl, loop controller</span>
<span class="token operator">-join</span><span class="token punctuation">(</span>0<span class="token punctuation">.</span><span class="token punctuation">.</span>1024<span class="token punctuation">|</span><span class="token operator">%</span><span class="token punctuation">{</span><span class="token namespace">[char]</span><span class="token namespace">[int]</span><span class="token punctuation">(</span><span class="token punctuation">(</span>48<span class="token punctuation">.</span><span class="token punctuation">.</span>57 <span class="token operator">+</span> 65<span class="token punctuation">.</span><span class="token punctuation">.</span>90 <span class="token operator">+</span> 97<span class="token punctuation">.</span><span class="token punctuation">.</span>122<span class="token punctuation">)</span><span class="token punctuation">|</span> <span class="token function">Get-Random</span><span class="token punctuation">)</span><span class="token punctuation">}</span><span class="token punctuation">)</span>

<span class="token operator">-join</span> <span class="token punctuation">(</span><span class="token namespace">[char[]]</span><span class="token punctuation">(</span>65<span class="token punctuation">.</span><span class="token punctuation">.</span>90<span class="token operator">+</span>97<span class="token punctuation">.</span><span class="token punctuation">.</span>122<span class="token punctuation">)</span> <span class="token punctuation">|</span> <span class="token function">Get-Random</span> <span class="token operator">-</span>Count 6<span class="token punctuation">)</span>

<span class="token function">Add-Type</span> <span class="token operator">-</span>AssemblyName System<span class="token punctuation">.</span>Web<span class="token punctuation">;</span><span class="token namespace">[System.Web.Security.Membership]</span>::GeneratePassword<span class="token punctuation">(</span>20<span class="token punctuation">,</span> 0<span class="token punctuation">)</span>

<span class="token keyword">function</span> <span class="token function">Get-RandomString</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token keyword">param</span><span class="token punctuation">(</span>
    <span class="token namespace">[int]</span><span class="token variable">$length</span>=10<span class="token punctuation">,</span>
    <span class="token comment"># 这里的[int]是类型指定</span>
    <span class="token namespace">[char[]]</span><span class="token variable">$sourcedata</span>
    <span class="token punctuation">)</span>

    <span class="token keyword">for</span><span class="token punctuation">(</span><span class="token variable">$loop</span>=1<span class="token punctuation">;</span> <span class="token variable">$loop</span> –le <span class="token variable">$length</span><span class="token punctuation">;</span> <span class="token variable">$loop</span>+<span class="token operator">+</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
            <span class="token variable">$TempPassword</span><span class="token operator">+=</span><span class="token punctuation">(</span><span class="token variable">$sourcedata</span> <span class="token punctuation">|</span> <span class="token function">GET-RANDOM</span> <span class="token punctuation">|</span> <span class="token operator">%</span><span class="token punctuation">{</span><span class="token namespace">[char]</span><span class="token variable">$_</span><span class="token punctuation">}</span><span class="token punctuation">)</span>
    <span class="token punctuation">}</span>
    <span class="token keyword">return</span> <span class="token variable">$TempPassword</span>
<span class="token punctuation">}</span>

<span class="token function">Get-RandomString</span> <span class="token operator">-</span>length 14 <span class="token operator">-</span>sourcedata <span class="token punctuation">(</span>48<span class="token punctuation">.</span><span class="token punctuation">.</span>127<span class="token punctuation">)</span>
</code></pre></div></div> <footer class="page-edit"><div class="edit-link"><a href="https://github.com/woytu/notes-vuepress/edit/master//Shell/PowerShell.md" target="_blank" rel="noopener noreferrer">在 GitHub 上编辑此页</a> <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></div> <div class="last-updated"><span class="prefix">上次更新:</span> <span class="time">2021-05-31 16:55:29</span></div></footer> <div class="page-nav"><p class="inner"><span class="prev">
      ←
      <a href="/Shell/" class="prev router-link-active">
        Shell
      </a></span> <span class="next"><a href="/Shell/ShellScript.html">
        ShellScript
      </a>
      →
    </span></p></div> </main></div><div class="global-ui"><!----></div></div>
    <script src="/assets/js/app.269009d6.js" defer></script><script src="/assets/js/3.974b9720.js" defer></script><script src="/assets/js/56.f29549a3.js" defer></script>
  </body>
</html>
